// pages/push/deal/index.js
import wxRequest from '../../../utils/wxRequest.js';
import utils from '../../../utils/util.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    group_id:null, //分组id区分新增修改
    group_name:null, //编辑时显示分组名
    visibleSelectLogic: false, //显示选择框
    group_array: ['广州男科'],
    group_index:0,
    status_array: ['正常', '暂封', '永封', '禁用'],
    status_index: 0,
    start_time: '12:01', //上线时间
    end_time: '10:01', //下线时间
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    if (options && options.group_id){
      that.setData({ group_id:options.group_id});

      wx.setNavigationBarTitle({
        title: '修改分组'
      })

      var map = {
        'id': options.group_id
      };
      wxRequest.getRequest('/toolkit/wechat_group/edit', map).then(res => {
        if (res.code == 0) {
          that.setData({ group_name:res.data.name})
        }
      }).catch(error => { })      
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  bindStartTimeChange(e) {//上线时间
    this.setData({
      start_time: e.detail.value
    })
  },
  bindEndTimeChange(e) {//下线时间
    this.setData({
      end_time: e.detail.value
    })
  },  
  // 选择分组
  bindPickerChange: function (e) {
    this.setData({
      group_index: e.detail.value
    })
  },
//选择状态
  statusChange: function (e) {
    console.log(e)
    this.setData({
      status_index: e.detail.value
    })
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  //提交
  submitFn:function(e){
    var that = this;
    if (that.data.group_id == null){
      //新增
      var map = {
        'name': e.detail.value.name
      };
      wxRequest.getRequest('/toolkit/wechat_group/save', map).then(res => {
        if(res.code == 0){
          wx.showToast({
            icon: 'success',
            title: res.message,
            duration: 1000,
            complete: function () {
              wx.redirectTo({
                url: '/pages/wechat/group/index',
              })
            }
          })
        }else{
          wx.showToast({
            icon: 'none',
            title: res.message,
            duration: 1000
          })
        }
        
      }).catch(error => { })
    }else{
      //修改
      var map = {
        'id': that.data.group_id,
        'name': e.detail.value.name
      };
      wxRequest.getRequest('/toolkit/wechat_group/update', map).then(res => {
        if (res.code == 0) {
          wx.showToast({
            icon: 'success',
            title: res.message,
            duration: 1000,
            complete: function () {
              wx.redirectTo({
                url: '/pages/wechat/group/index',
              })
            }
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.message,
            duration: 1000
          })
        }

      }).catch(error => { })
    }
  }
   
})